<template>
  <div class="logisticsCompanyList">
    <filterPanel filterName="CLogisticsCompanyListFilter" filterTitle="物流公司查询" :filterList="filterData"
                 @filterChange="filterChange"></filterPanel>
    <tablePanel tableName="CLogisticsCompanyListTable" :tableLoading="loading" tableTitle="物流公司列表"
                :tableList="tableList"
                :tableData="tableData" :total="total" :currentPage="form.page_start" :pageSize="form.page_size"
                @sizeChange="sizeChange" @pageChange="pageChange" @tableClick="tableClick">
      <zy-button type="primary" @click="tableClick('', 'add')">新增</zy-button>
    </tablePanel>
    <companyModel :is-show="showModel" @submit="modelSubmit" @close="hideModel"></companyModel>
  </div>
</template>

<script>
	import companyModel from "./companyModel";

	export default {
		name: "CLogisticsCompanyList",
		props: {},
		data() {
			return {
				filterData: [
					{
						title: '企业名称',
						name: 'company_name',
						type: 'input',
						value: '',
						maxlength: '50',
						isShow: true,
						isSelected: true
					},
					{
						title: '负责人',
						name: 'control_man',
						type: 'input',
						value: '',
						maxlength: '20',
						isShow: true,
						isSelected: true
					},
					{
						title: '联系方式',
						name: 'control_phone',
						type: 'input',
						value: '',
						maxlength: '11',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						name: 'time',
						type: 'date',
						value: [],
						isShow: true,
						isSelected: true
					},
					{
						title: '创建人',
						name: 'create_user_name',
						type: 'input',
						value: '',
						maxlength: '20',
						isShow: true,
						isSelected: true
					},
				],
				form: {
					company_name: "",
					control_man: "",
					control_phone: "",
					create_user_name: "",
					time: [],
					page_start: 1,
					page_size: 10
				},
				loading: false,
				tableList: [
					{
						title: '企业名称',
						name: 'company_name',
						isShow: true,
						isSelected: true
					},
					{
						title: '负责人',
						name: 'control_man',
						isShow: true,
						isSelected: true
					},
					{
						title: '联系方式',
						name: 'control_phone',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建人',
						name: 'user_name',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						name: 'crete_time_desc',
						isShow: true,
						isSelected: true
					},
					{
						title: '操作',
						name: 'handler',
						fixed: 'right',
						limitWidth: 160,
						btns: [
							{
								type: 'look',
								name: '查看'
							},
							{
								type: 'delete',
								name: '删除',
								btnType: 'danger'
							},
						],
						isSelected: true
					}
				],
				tableData: [],
				total: 0,
				showModel: false
			};
		},
		computed: {
			postData() {
				let data = {
					...this.form,
					pagination: {
						page_start: this.form.page_start,
						page_size: this.form.page_size
					}
				};
				if (this.form.time && this.form.time.length > 0) {
					data.create_time_start = this.form.time[0] / 1000;
					data.create_time_end = this.form.time[1] / 1000;
				}
				return data;
			},
		},
		watch: {},
		components: {
			companyModel
		},
		methods: {
			sizeChange(val) {
				this.form.page_size = val;
				this.getData()
			},
			pageChange(val) {
				this.form.page_start = val;
				this.getData()
			},
			filterChange(data) {
				this.form = {...this.form, ...data};
				this.getData();
			},
			getData() {
				this.loading = true;
				this.tableData = [];
				this.$axios.post(this.$api.c_getCompanyList, this.postData).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.tableData = data.list.filter(item => {
							item.look = true;
							item.delete = true;
							return item;
						});
						this.total = data.pagination.total_count || 0;
					} else {
						this.total = 0;
						this.tableData = [];
					}
				}).catch(ret => {
					this.loading = false;
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			tableClick(data, type) {
				if (type === 'delete') {
					this.$confirm("此操作将删除该企业, 是否继续?", "提示", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning",
						customClass: 'message',
					}).then(() => {
						this.saveCompany(data.ccr_id, 1)
					}).catch(() => {
						this.$message({type: "info", message: "已取消删除"});
					});
				} else if (type === 'add') {
					this.showModel = true;
				} else {
					this.$toRoutePath({
						path: "/cooperate/logistics_company/detail",
						edit: true,
						query: {
							type: type,
							company_id: data.company_id
						}
					});
				}
			},
			modelSubmit(val) {
				if (val && val.company_id) {
					this.saveCompany(val.company_id, 0)
				} else {
					this.$message.error('请选择需要添加的企业');
				}
			},
			hideModel() {
				this.showModel = false;
			},
			saveCompany(company_ids, type) {
				this.$axios.post(this.$api.c_saveCompany, {
					company_ids: company_ids,
					type: type
				}).then(res => {
					let {code, msg} = res;
					if (code == 200) {
						this.$message.success(msg);
						this.getData();
						this.hideModel();
					} else {
						this.$message.error(msg);
					}
				})
			},
			exportUrl() {
			}
		},
		mounted() {
			this.getData()
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/cooperate/logisticsCompany/logisticsCompanyList/index.less">

</style>
